"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 二级分区
Case Name   : range_hash二级分区表:check约束/index_parameters
Create At   : 2022/2/24
Owner       : @daiguatutu
Description :
    1.创建二级分区表,包含check约束
    2.插入不符合约束的数据
    3.插入符合约束数据
    4.创建二级分区表，分区列指定unique
    5.插入正确数据并查询
    6.插入违反约束的数据
    7.创建二级分区表，指定表约束primary key
    8.查看索引
    9.插入正确数据并查询
    10.插入违反约束的数据
    11.清理环境
Expect      :
    1.成功
    2.合理报错
    3.成功
    4.成功
    5.成功
    6.合理报错
    7.成功
    8.成功
    9.成功
    10.合理报错
    11.清理成功
History     :
"""
import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Subpartition(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.t_name = 't_subpartition_0312'
        self.t_name_01 = 't_subpartition_0312_01'
        self.i_name_01 = 'i_subpartition_0312_01'
        self.i_name_02 = 'i_subpartition_0312_02'

    def test_subpartition(self):
        text = '-----step1:创建二级分区表,包含check约束 expect:成功-----'
        self.log.info(text)
        create_sql = f"drop table if exists {self.t_name};" \
            f"create table if not exists {self.t_name}(" \
            f"col_1 int ,col_2 int not null,col_3 int not null,col_4 int," \
            f"check (col_1 > col_2 and col_4 is null) ) " \
            f"partition by range (col_1) subpartition by hash (col_2)" \
            f"( partition p_range_1 values less than( -10 )" \
            f"  ( subpartition p_hash_1_1 , subpartition p_hash_1_2 ," \
            f"    subpartition p_hash_1_3)," \
            f"  partition p_range_2 values less than( 20 )," \
            f"  partition p_range_3 values less than( 30)" \
            f"  ( subpartition p_hash_3_1 , subpartition p_hash_3_2 ," \
            f"    subpartition p_hash_3_3)," \
            f"    partition p_range_4 values less than( 50)" \
            f"  ( subpartition p_hash_4_1 , subpartition p_hash_4_2 ," \
            f"    subpartition t_subpartition_0350)," \
            f"  partition p_range_5 values less than( maxvalue )" \
            f") enable row movement;"
        msg = self.pri_sh.execut_db_sql(create_sql)
        self.log.info(msg)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, msg, '执行失败:' + text)

        text = '-----step2:插入不符合约束的数据 expect:合理报错-----'
        self.log.info(text)
        insert_sql = f"insert into {self.t_name} values(2,1,1),(5,5,5);" \
            f"insert into {self.t_name} values(2,1,1,1),(6,5,5,5);"
        msg = self.pri_sh.execut_db_sql(insert_sql)
        self.log.info(msg)
        self.assertIn('violates check constraint', msg, '执行失败:' + text)

        text = '-----step3:插入符合约束数据 expect:成功-----'
        self.log.info(text)
        insert_sql = f"insert into {self.t_name} values(2,1,1),(6,5,5);"
        msg = self.pri_sh.execut_db_sql(insert_sql)
        self.log.info(msg)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, msg, '执行失败:' + text)

        text = '-----step4:创建二级分区表，分区列指定unique expect:成功-----'
        self.log.info(text)
        create_sql = f"drop table if exists {self.t_name};" \
            f"create table {self.t_name} (col_1 int, col_2 int not null , " \
            f"col_3 int not null , col_4 int,unique (col_1,col_2))" \
            f"partition by range (col_1) subpartition by hash (col_2)" \
            f"( partition p_range_1 values less than( -10 )" \
            f"  ( subpartition p_hash_1_1 , subpartition p_hash_1_2 ," \
            f"    subpartition p_hash_1_3)," \
            f"  partition p_range_2 values less than( 20 )," \
            f"  partition p_range_3 values less than( 30)" \
            f"  ( subpartition p_hash_3_1 , subpartition p_hash_3_2 ," \
            f"    subpartition p_hash_3_3)," \
            f"    partition p_range_4 values less than( 50)" \
            f"  ( subpartition p_hash_4_1 , subpartition p_hash_4_2 ," \
            f"    subpartition t_subpartition_0350)," \
            f"  partition p_range_5 values less than( maxvalue )" \
            f") enable row movement;"
        msg = self.pri_sh.execut_db_sql(create_sql)
        self.log.info(msg)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, msg, '执行失败:' + text)

        text = '-----step5:插入正确数据并查询 expect:成功-----'
        self.log.info(text)
        sql = f"insert into {self.t_name} values(1,1,1,1),(5,5,5,5);" \
            f"select * from {self.t_name};"
        msg = self.pri_sh.execut_db_sql(sql)
        self.log.info(msg)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, msg, '执行失败:' + text)
        self.assertIn('2 rows', msg, '执行失败:' + text)

        text = '-----step6:插入违反约束的数据 expect:合理报错-----'
        self.log.info(text)
        msg = self.pri_sh.execut_db_sql(sql)
        self.log.info(msg)
        self.assertIn('violates unique constraint', msg, '执行失败:' + text)

        text = '-----step7:创建二级分区表，指定表约束primary key expect:成功-----'
        self.log.info(text)
        create_sql = f"drop table if exists {self.t_name};" \
            f"create table {self.t_name} (col_1 int, col_2 int not null , " \
            f"col_3 int not null , col_4 int ," \
            f"primary key (col_1,col_2) deferrable)" \
            f"partition by range (col_1) subpartition by hash (col_2)" \
            f"( partition p_range_1 values less than( -10 )" \
            f"  ( subpartition p_hash_1_1 , subpartition p_hash_1_2 ," \
            f"    subpartition p_hash_1_3)," \
            f"  partition p_range_2 values less than( 20 )," \
            f"  partition p_range_3 values less than( 30)" \
            f"  ( subpartition p_hash_3_1 , subpartition p_hash_3_2 ," \
            f"    subpartition p_hash_3_3)," \
            f"    partition p_range_4 values less than( 50)" \
            f"  ( subpartition p_hash_4_1 , subpartition p_hash_4_2 ," \
            f"    subpartition t_subpartition_0350)," \
            f"  partition p_range_5 values less than( maxvalue )" \
            f") enable row movement;"
        msg = self.pri_sh.execut_db_sql(create_sql)
        self.log.info(msg)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, msg, '执行失败:' + text)

        text = '-----step8:查看索引 expect:成功-----'
        self.log.info(text)
        msg = self.pri_sh.execut_db_sql('\di')
        self.log.info(msg)
        self.assertIn(f'{self.t_name}_pkey', msg, '执行失败:' + text)

        text = '-----step9:插入正确数据并查询 expect:成功-----'
        self.log.info(text)
        sql = f"insert into {self.t_name} values(1,1,1,1),(5,5,5,5);"
        msg = self.pri_sh.execut_db_sql(sql)
        self.log.info(msg)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, msg, '执行失败:' + text)

        text = '-----step10:插入违反约束的数据 expect:合理报错-----'
        self.log.info(text)
        msg = self.pri_sh.execut_db_sql(sql)
        self.log.info(msg)
        self.assertIn('violates unique constraint', msg, '执行失败:' + text)

    def tearDown(self):
        self.log.info('-----step11:清理环境-----')
        text_1 = '-----删除表 expect:成功-----'
        self.log.info(text_1)
        del_cmd = f'drop table if exists {self.t_name};' \
            f'drop table if exists {self.t_name_01};'
        msg = self.pri_sh.execut_db_sql(del_cmd)
        self.log.info(msg)

        self.assertEqual(2, msg.count(self.constant.TABLE_DROP_SUCCESS),
                         '执行失败:' + text_1)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
